package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.DatoscomplementariosDAO;
import py.com.alfa.escriba.persistence.dao.ReferenciaDAO;
import py.com.alfa.escriba.persistence.dao.VehiculosinfoDAO;
import py.com.alfa.escriba.persistence.dto.VehiculosinfoDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.VehiculosinfoService;

public class VehiculosinfoServiceImpl implements VehiculosinfoService{

  private VehiculosinfoDAO vehiculosinfoDAO;
  private DatoscomplementariosDAO datoscomplementariosDAO;
  private ReferenciaDAO referenciaDAO;

  public void setVehiculosinfoDAO(VehiculosinfoDAO vehiculosinfoDAO) {
    this.vehiculosinfoDAO = vehiculosinfoDAO;
  }
  
  public void setDatoscomplementariosDAO(DatoscomplementariosDAO datoscomplementariosDAO) {
    this.datoscomplementariosDAO = datoscomplementariosDAO;
  }
  
  public void setReferenciaDAO(ReferenciaDAO referenciaDAO) {
		this.referenciaDAO = referenciaDAO;
	}

  @Override
  public List<VehiculosinfoDTO> listVehiculosinfo(InvocationContext ic) throws ServiceException {
    List<VehiculosinfoDTO> vehiculosinfos =  vehiculosinfoDAO.listVehiculosinfo();
    for (VehiculosinfoDTO vi : vehiculosinfos) {
		vi.setDatoComplementario(datoscomplementariosDAO.getDatoscomplementariosById(vi.getIddato()));
		Long idtipodato = new Long(vi.getDatoComplementario().getIdtipodato());
		vi.getDatoComplementario().setTipoDato(referenciaDAO.getTabRefById(idtipodato));
	}
    return vehiculosinfos;
  }

  @Override
  public VehiculosinfoDTO getVehiculosinfo(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    VehiculosinfoDTO vehiculosinfo =  vehiculosinfoDAO.getVehiculosinfoById(id);

    if (vehiculosinfo == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"vehiculosinfo.notfound", id);

    return vehiculosinfo;
  }

  @Override
  public void deleteVehiculosinfo(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    VehiculosinfoDTO vehiculosinfo =  vehiculosinfoDAO.getVehiculosinfoById(id);

    if(vehiculosinfo == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"vehiculosinfo.notfound", id);

    try {
      vehiculosinfoDAO.deleteVehiculosinfo(vehiculosinfo);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "vehiculosinfo.constraint.violation", id);
    }

  }

  @Override
  public VehiculosinfoDTO updateVehiculosinfo(Long id, VehiculosinfoDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    VehiculosinfoDTO vehiculosinfo = vehiculosinfoDAO.getVehiculosinfoById(id);

    if (vehiculosinfo == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"vehiculosinfo.notfound", id);

    vehiculosinfo.setIdvehiculo(dto.getIdvehiculo());
    vehiculosinfo.setIddato(dto.getIddato());
    vehiculosinfo.setValor(dto.getValor());
    vehiculosinfo.setIdusuario(dto.getIdusuario());
    vehiculosinfo.setFechacreacion(dto.getFechacreacion());
    vehiculosinfo.setIdusuariomodificacion(dto.getIdusuariomodificacion());
    vehiculosinfo.setFechamodificacion(dto.getFechamodificacion());
    vehiculosinfo.setTrn(dto.getTrn());
 

    Utils.validarDTO(dto);

    vehiculosinfoDAO.updateVehiculosinfo(dto);

    return vehiculosinfo;
  }

  @Override
  public VehiculosinfoDTO createVehiculosinfo(VehiculosinfoDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      vehiculosinfoDAO.createVehiculosinfo(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "vehiculosinfo.duplicate.key");
    }

    return dto;
  }

}